package com.luca.sl651server.biz.service.impl;

import com.luca.sl651server.biz.service.BizService;
import com.luca.sl651server.util.HexStringUtil;
import com.luca.sl651server.dto.req.FrameReqWrapper;
import com.luca.sl651server.mqtt.message.MqttGateway;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class BizServiceImpl implements BizService {
    private static final Logger LOGGER = LoggerFactory.getLogger(BizServiceImpl.class);

    @Autowired
    private MqttGateway mqttGateway;

    @Value("${mqtt.default.topic}")
    private String defaultTopic;

    @Override
    public boolean handler(FrameReqWrapper frameReqWrapper) {
//        // 发送到mqtt
        try {
        // 发布渗压消息
        String msg = frameReqWrapper.getOriginalFrame();
        byte[] bytes = HexStringUtil.hexStr2Bytes(msg);
        mqttGateway.sendToMqtt(defaultTopic, bytes);
        } catch (Exception e) {
            LOGGER.error("[MQTT 发送渗压消息出错]  frameReqWrapper is:{}", frameReqWrapper, e);
            return false;
        }
        return true;
    }

}
